<template>
  <div>
    <el-dialog title="新建用户" :visible.sync="dialogFormVisible">
      <div class="tfscrollbarstyle" style="height:400px;">
        <el-form ref="dataForm" :model="temp"  label-position="right" label-width="100px" style='width: 496px; margin-left:50px;'>
          <el-form-item label="用户名" prop="userName" :rules="[{ required: true, message: '用户名不能为空', trigger: 'blur' },
            {min:3, max:16, message:'必须3-16个字符', trigger:'blur'},
            { trigger: 'blur', validator: validateUsername }]">
            <el-input v-model="temp.userName" size="mini"></el-input>
          </el-form-item>
          <div style="margin-left: 100px" >
            <span>用户名填写规则：</span><br/>
            <span>1)由汉字、英文字母（不区分大小写）、数字、点或下划线组成。</span><br/>
            <span>2)长度为4-16个字符（每个英文字母或数字算一个字符，每个汉字算两个字符）。</span><br/>
            <span>3)system、admin是系统保留账号。</span>
          </div>
          <el-form-item label="真实姓名" prop="trueName" :rules="filter_rules({required: true, max: 50, min: 3, type: 'trueName'})">
            <el-input v-model="temp.trueName"  size="mini"></el-input>
          </el-form-item>
          <el-form-item  label="密码" prop="password" :rules="filter_rules({required: true, max: 20, min: 3})">
            <el-input v-model="temp.password" type="password"  size="mini"></el-input>
          </el-form-item>
          <el-form-item  label="确认密码" prop="checkPassword" :rules="[{ required: true, message: '密码不能为空', trigger: 'blur' },
            { trigger: 'blur', validator: validatePass2 }]">
            <el-input v-model="temp.checkPassword" type="password"  size="mini"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile" :rules="filter_rules({required: true, max: 11, type: 'mobile'})">
            <el-input v-model="temp.mobile"  size="mini"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" prop="detailAddress" :rules="filter_rules({ maxLength: 50})">
            <el-input v-model="temp.detailAddress"  size="mini"></el-input>
          </el-form-item>
          <el-form-item label="QQ：" prop="qq" :rules="filter_rules({ maxLength: 11, type: 'qq'})">
            <el-input v-model="temp.qq"  size="mini"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱：" prop="email" :rules="filter_rules({ maxLength: 50, type: 'email'})">
            <el-input v-model="temp.email" type="email"  size="mini"></el-input>
          </el-form-item>
          <el-form-item label="微信账号：" prop="wechat" :rules="filter_rules({ maxLength: 11})">
            <el-input v-model="temp.wechat"  size="mini"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"  size="mini">取 消</el-button>
        <el-button  type="primary" @click="createData" size="mini">确 定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script type="text/javascript">
  import {createUserToAdminRemote} from '@/view/api/organization.js'
  import {yfuPost} from '@/utils/request.js'
  import {getUserByUsernameRemote} from '@/view/api/userManager.js'
  import { encrypt, encryptParameter } from '@/utils/des.js'
  export default {
    data () {
      return {
        activeName: 'first',
        dialogFormVisible: false,
        dialogPvVisible: false,
        temp: {}
      }
    },
    methods: {
      validatePass2 (rule, value, callback) {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.temp.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      validateUsername (rule, value, callback) {
        if (value === '') {
          callback(new Error('请输入用户名'))
        } else {
          let myreg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){3,16}$/
          if (!myreg.test(value)) {
            callback(new Error('您输入的用户名格式不正确!'))
          } else {
            let param = {}
            param.userName = value
            yfuPost(process.env.BASE_API, getUserByUsernameRemote.url, param, (data) => {
              if (data === '用户可以注册') {
                callback()
              } else {
                callback(new Error('用户名已存在'))
              }
            })
          }
        }
      },
      handleClick () {

      },
      createUserDialog () {
        this.dialogFormVisible = true
        this.temp = {}
      },
      createData () {
        let cThis = this
        let params = {}
        cThis.temp.groupids = this.$store.getters.currentOrganizationId

        params.userName = cThis.temp.userName
        params.trueName = cThis.temp.trueName
        params.password = encrypt(encryptParameter.key, encryptParameter.iv, cThis.temp.password)
        params.checkPassword = encrypt(encryptParameter.key, encryptParameter.iv, cThis.temp.checkPassword)
        params.mobile = cThis.temp.mobile
        params.detailAddress = cThis.temp.detailAddress
        params.qq = cThis.temp.qq
        params.email = cThis.temp.email
        params.wechat = cThis.temp.wechat
        params.groupids = cThis.temp.groupids

        console.log(params)
        console.log(cThis.temp)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            yfuPost(process.env.BASE_API, createUserToAdminRemote.url, params, (data) => {
              cThis.dialogFormVisible = false
              cThis.$emit('on-refresh-change')
            })
          } else {
            alert(1)
            return false
          }
        })
      }
    }
  }
</script>

<style>

</style>
